﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessLand
    {
        #region Create
        public static void createLand(Land l)
        {
            try
            {
                dataLand.createLand(l);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating land");
            }
        }

        public static void createLand(User u, Zone z, Region r, String name)
        {
            try
            {
                Land l = new Land();
                l.UserID = u.UserID;
                l.ZoneID = z.ZoneID;
                l.Name = name;
                l.State = true;

                dataLand.createLand(l);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating land");
            }
        }

        public static void createLand(User u, Zone z, Region r, String name, float length, float width, float acre)
        {
            try
            {
                Land l = new Land();
                l.UserID = u.UserID;
                l.ZoneID = z.ZoneID;
                l.Name = name;
                l.Length = length; // meter
                l.Width = width; // meter
                l.Acre = acre;
                l.State = true;

                dataLand.createLand(l);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating land");
            }
        }

        public static void insertPlantInLand(Plant p, Land l)
        {
            try
            {
                Plants_Land pl = new Plants_Land();
                pl.LandID = pl.LandID;
                pl.PlantID = pl.PlantID;

                dataLand.createPlant_Land(pl);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error inserting plant in a land");
            }
        }

        public static void createLandReading(Land l, float pH, float k, float p, float na)
        {
            try
            {
                Reading r = new Reading();
                r.LandID = l.LandID;
                r.pH = pH;
                r.Phosphorus = k;
                r.Potassium = p;
                r.Nitrogen = na;

                dataReading.createReading(r);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating a reading for land");
            }
        }
        #endregion

        #region Get
        public static List<Land> getLandsByUser(User u)
        {
            try
            {
                Expression<Func<Land, bool>> whereClause = (Land l) => (l.UserID == u.UserID) && (l.State == true);

                return dataLand.getLands(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting land by user");
            }
        }

        public static List<Plant> getPlantsByLand(Land l)
        {
            try
            {
                List<Plant> plants = new List<Plant>();
                
                Expression<Func<Plants_Land, bool>> whereClause = (Plants_Land pl) => pl.LandID == l.LandID;
                List<Plants_Land> plants_land = dataLand.getLand_Plants(whereClause).ToList();
               
                foreach (Plants_Land pl in plants_land)
                {
                    Plant p = dataPlant.getPlant(pl.PlantID);

                    if (!plants.Contains(p))
                    {
                        plants.Add(p);
                    }
                }

                return plants;
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting plants by land");
            }
        }
        #endregion

        #region Set
        public static Boolean changeLand(Land l)
        {
            try
            {
                return dataLand.changeLand(l);
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error changing land");
            }
        }

        public static Boolean activeLand(Land l)
        {
            try
            {
                l.State = true;
                return dataLand.changeLand(l);
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error activing land");
            }
        }

        public static Boolean inactiveLand(Land l)
        {
            try
            {
                l.State = false;
                return dataLand.changeLand(l);
            }
            catch (Exception ex)
            {
                Messages.AddDebug(ex.Message);
                throw new Exception("Error inactiving land");
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteLand(Land l)
        {
            try
            {
                return dataLand.deleteLand(l);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error deletting land");
            }
        }

        public static Boolean deletePlantInLand(Plants_Land pl)
        {
            try
            {
                return dataLand.deletePlant_Land(pl);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error deletting a plant in a land");
            }
        }
        #endregion
    }
}
